using Resoure.Script.Utilities;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Events;
using UnityEngine.Rendering.Universal;

public class SelectorController : MonoBehaviour,Interface_Interactive
{
    public GameObject selector;
    public GameObject target;
    public SpriteRenderer SelectorSpriteRenderer;
    public Light2D SelectorLight;
    
    public bool isCanUse = false;
    public bool isFocus = false;
    public bool isBeUsed = false;
    public bool isDisposable = false;
    public Vector2 PlayerPostion;

    public Collider2D player;

    public float DisplayDistance;
    public float VisibleDistance;
    public float CanPressDistance;
    public float PlayerDistance;

    private Animator animator;
    
    public UnityEvent OnInteractive;

    private void Awake()
    {
        animator = selector.GetComponent<Animator>();
        SelectorSpriteRenderer= selector.GetComponent<SpriteRenderer>();
        SelectorLight = selector.GetComponent<Light2D>();
        selector.SetActive(false);
    }

    private void Update()
    {
        animator.SetBool("IsFocus",(isFocus&&isCanUse));
        IsCanUse();
        SpriteVisible();
        IsFocus();
        IsClick();
        if (isBeUsed)
        {
            DestroyItem();
        }
    }



    private void OnTriggerStay2D(Collider2D other)
    {
        if (other.CompareTag("Player"))
        {
            player = other;
            PlayerDistance = Vector2.Distance(other.transform.position, target.transform.position);
            
            selector.SetActive(true);
        }
    }
    

    private void OnTriggerExit2D(Collider2D other)
    {
        if (other.CompareTag("Player"))
        {
            selector.SetActive(false);
            player = null;
        }
    }

    public void TriggerAction()
    {
        Debug.Log("Use");
        OnInteractive?.Invoke();
        if (isDisposable)
        {
            DestroyItem();
        }
    }

    public void IsFocus()
    {
        // 获取鼠标当前的屏幕坐标
        Vector2 mouseScreenPosition = Mouse.current.position.ReadValue();

        // 将屏幕坐标转换为世界坐标
        Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(new Vector3(mouseScreenPosition.x, mouseScreenPosition.y, 0));

        if (target.transform.position.x - mouseWorldPosition.x < CanPressDistance &&
            target.transform.position.x - mouseWorldPosition.x > -CanPressDistance)
        {
            if (target.transform.position.y - mouseWorldPosition.y < CanPressDistance &&
                target.transform.position.y - mouseWorldPosition.y > -CanPressDistance)
            {
                isFocus= true;
            }
        }
        else
        {
            isFocus = false;
        }
    }

    public void IsClick()
    {
        if (isFocus && Input.GetMouseButtonDown(0)&&isCanUse)
        {
            TriggerAction();
        }
    }

    public void IsCanUse()
    {
        if (PlayerDistance < CanPressDistance &&player!= null)
        {
            isCanUse = true;
        }
        else
        {
            isCanUse = false;
        }
    }
    public void SpriteVisible()
    {
        // 确保 PlayerDistance 在 DisplayDistance 和 VisibleDistance 之间
        if (PlayerDistance >= VisibleDistance && PlayerDistance <= DisplayDistance)
        {
            // 计算可见度的比例
            float visibilityRatio = 1 - (PlayerDistance - VisibleDistance) / (DisplayDistance - VisibleDistance);
        
            // 通过改变 Color 的 alpha 值来控制透明度
            Color spriteColor = SelectorSpriteRenderer.color;
            spriteColor.a = visibilityRatio;  // 设置透明度
            SelectorLight.intensity = visibilityRatio;
            SelectorSpriteRenderer.color = spriteColor;
        }
        else if (PlayerDistance < VisibleDistance)
        {
            // 玩家距离小于 VisibleDistance，完全可见
            Color spriteColor = SelectorSpriteRenderer.color;
            spriteColor.a = 1f;  // 完全不透明
            SelectorLight.intensity = 1f;
            SelectorSpriteRenderer.color = spriteColor;
        }
        else
        {
            // 玩家距离大于 DisplayDistance，完全不可见
            Color spriteColor = SelectorSpriteRenderer.color;
            spriteColor.a = 0f;  // 完全透明
            SelectorLight.intensity = 0f;
            SelectorSpriteRenderer.color = spriteColor;
        }
    }

    

    public void DestroyItem()
    {
        Destroy(selector);
        Destroy(target);
    }
}
